<?php

header("Content-Type: text/html; charset=utf-8"); 
require_once('sessionVal.php');

require("../config.inc.php");
require_once(COMM_PATH."BMS.php");

function deldir($dir) {
  //先删除目录下的文件：
  $dh=opendir($dir);
  while ($file=readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(!is_dir($fullpath)) {
          unlink($fullpath);
      } else {
          deldir($fullpath);
      }
    }
  }
 
  closedir($dh);
  //删除当前文件夹：
  if(rmdir($dir)) {
    return true;
  } else {
    return false;
  }
}

/**
 * 删除文件
 * Enter description here ...
 * @param unknown_type $img
 */
function deleteImg($img) {
	if(file_exists($img)){
		unlink($img);
	}
}

$bms = new BMS();
$cateID = $_GET['F_ID'];


if($cateID==null || $cateID==""){
	echo 'ID为空！';
	echo "<a href='booklist.php'>返回</a>";
	exit();
}else {
	$book = $bms->getInfo($cateID, $bms->_BOOK);
	if ($book[0]["path"]){
		$flag = deldir(INCLUDE_PATH.$book[0]["path"]);//删除目录以及文件
	}else {
		echo "<script type=\"text/javascript\">alert(\"删除失败！\");window.location.href=\"booklist.php\";</script>";
		exit;
	}
	if ($book[0]["imagePath"]){
		deleteImg(INCLUDE_PATH."IMAGE_FILE/".$book[0]["imagePath"]);//删除大图片
		deleteImg(INCLUDE_PATH."IMAGE_FILE/min/".$book[0]["imagePath"]);//删除小图片
	}else{
		echo "<script type=\"text/javascript\">alert(\"删除失败！\");window.location.href=\"booklist.php\";</script>";
		exit;
	}
	if($flag){
		if($bms->delData($bms->_BOOK,$cateID)){
			echo "<script type=\"text/javascript\">alert(\"删除成功！\");window.location.href=\"booklist.php\";</script>";
			exit;
		}else{
			echo "<script type=\"text/javascript\">alert(\"删除失败！\");window.location.href=\"booklist.php\";</script>";
			exit;
		}
	}else{
		echo "<script type=\"text/javascript\">alert(\"文件删除失败！\");window.location.href=\"booklist.php\";</script>";
		exit;
	}
	
}
?>